#include "wl_trace.h"
#include "wl_utils.h"

#include <string.h>

#define CUR_WLLOG_LEVEL WLLOG_INFO_LEVEL
#if defined(WL_OS_ANYKA_LINUX)
#define TRACE_BUF_SIZE 2048
#else
#define TRACE_BUF_SIZE 256
#endif

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

static wl_trace_callback s_trace_callback;
static wl_char           s_trace_level[WLLOG_LEVEL_MAX][10] = {"TRACE", "INFO", "DEBUG", "WARN", "ERROR"};

void wl_trace_regist_callback(wl_trace_callback callback) {
    s_trace_callback = callback;
}

void wl_trace(wl_int32 level, wl_char* format, ...) {
    if (level < WLLOG_LEVEL_MAX && level >= CUR_WLLOG_LEVEL && wl_null != s_trace_callback) {
        va_list  va;
        wl_char  buf[TRACE_BUF_SIZE] = "";
        wl_int32 buflen              = 0;

        sprintf(buf, "[%d][%d][WLSDK][%s]:", wl_utctime(), wl_getElapsedTick(), s_trace_level[level]);
        buflen = strlen(buf);

        va_start(va, format);
        vsnprintf(buf + buflen, TRACE_BUF_SIZE - buflen - 1, format, va);
        va_end(va);
        buf[255] = '\0';

        s_trace_callback(buf);
    }
}

#ifdef __cplusplus
}
#endif /* __cplusplus */
